﻿using System;
using System.Drawing;

namespace T.SF.Console
{
    public enum LogShowType : int 
    {
        FailInfo,
        SuccessInfo,
        GeneralInfo
    }
    public static class ConsoleService
    {
        private static bool setConsoleColor;

        public static void Show(string msg, LogShowType type = LogShowType.GeneralInfo)
        {
            if (type == LogShowType.GeneralInfo)
                System.Console.WriteLine(msg);
            else
            {
                ConsoleColor consoleColor = GetColor(type);
                SetConsoleColor(consoleColor);
                System.Console.WriteLine(msg);
                System.Console.ResetColor();
            }
        }

        public static void Show(Exception exception)
        {
            if (exception == null)
                return;

            SetConsoleColor(ConsoleColor.Red);
            System.Console.WriteLine(exception.Message);
            System.Console.ResetColor();
            System.Console.WriteLine(exception.StackTrace);
            if (exception.InnerException != null)
                Show(exception.InnerException);
        }

        private static ConsoleColor GetColor(LogShowType type)
        {
            switch (type)
            {
                case LogShowType.FailInfo:
                    return ConsoleColor.Red;
                case LogShowType.SuccessInfo:
                    return ConsoleColor.Green;
                default:
                case LogShowType.GeneralInfo:
                    return ConsoleColor.Black;
            }
        }


        public static void CheckCanSetConsoleColor()
        {
            try
            {
                System.Console.ForegroundColor = ConsoleColor.Green;
                System.Console.ResetColor();
                setConsoleColor = true;
            }
            catch
            {
                setConsoleColor = false;
            }
        }

        public static void SetConsoleColor(ConsoleColor color)
        {
            if (setConsoleColor)
                System.Console.ForegroundColor = color;
        }

        public static void ResetColor()
        {
            System.Console.ResetColor();
        }
    }
}
